ZhanFuYingScene={}
ZhanFuYingSceneFunc={}
ZhanFuYingSceneFunc.__index = ZhanFuYingSceneFunc
setmetatable(ZhanFuYingScene, ZhanFuYingSceneFunc)
ZhanFuYingScene.NodeID = {UpButton=1020}
--ZhanFuYingScene.upgradeCooldown = 0

ZhanFuYingSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, ZhanFuYingSceneFunc)
  l.node = CCBReader:load("ZhanFuYingScene.ccbi")
  print("ZhanFuYingScene.getNode", l.node)
  ZhanFuYingScene.instance = l
  l:init() 
  return l 
end

ZhanFuYingSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ZhanFuYingScene.getNode().node)
  print("ZhanFuYingScene.getScene", sc)
  return sc
end

ZhanFuYingSceneFunc.init = function(self)
  local b = self.node:getChildByTag(ZhanFuYingScene.NodeID.UpButton)
  setObjectExtTable(b, self)
  b = self.node:getChildByTag(1111)
  setObjectExtTable(b, self)

  
  setBuildingBaseData(BuildingID.ZhanFuYing, self.node)
  self:initTable()
  --UpgradeManager.checkUpgrade(BuildingID.ZhanFuYing)
  self.upBuildingButton = TypeCast(self.node:getChildByTag(1020), "CCButton")
  self.timeLabel = TypeCast(self.node:getChildByTag(1006), "CCLabelTTF")
  local tmp = UITimer.getUITimer(string.format("upbuildingcd_%d", BuildingID.ZhanFuYing), self, ccp(160,72), ccp(0, 0))
  if tmp~=nil then
    tmp.view:setPosition(ccp(710, 420))
    self.node:addChild(tmp.view)
    self.upBuildingTimer = tmp
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
    self.upBuildingButton:setEnabled(false)
  end
end

ZhanFuYingSceneFunc.initTable = function(self)
  self.fuluItems = {}
  self.buttons = {}
  local items = CCArray:create()
  local buildingLevel = UserData.userinfo.host.cities2[tostring(BuildingID.ZhanFuYing)].l
  local count = UserData.config.models.CampPowLevel[buildingLevel].powMax
  local count1 = count
  if UserData.config.models.CampPowLevel[buildingLevel+1]~=nil then
    count1 = UserData.config.models.CampPowLevel[buildingLevel+1].powMax
  end
  local tmp = string.format("当前可俘虏战俘数: %d", count)
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(tmp)
  tmp = string.format("下一级可俘虏战俘数: %d", count1)
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(tmp)

  for i=1,count  do
    local item = CCBReader:load("ZhanFuItem.ccbi")
    setObjectExtTable(item:getChildByTag(1020), self)
    item:setContentSize(CCSizeMake(320, 190))
    items:addObject(item)
    self.fuluItems[#self.fuluItems+1] = item
  end
  local c=0
  --for k,v in pairs(UserData.userinfo.host.occupyInfo.occupy) do
  print("ZhanFuScene",UserData.userinfo.host.occupyInfo.occupy, UserData.userinfo.captures)
  --if UserData.userinfo.captures~=nil then
  if UserData.userinfo.host.occupyInfo.occupy~=nil then
  for k,v in pairs(UserData.userinfo.host.occupyInfo.occupy) do
    local item = items:objectAtIndex(c)
    --local sp = CCSprite:create(imgName)
    local info = nil
    for m,n in pairs(UserData.userinfo.captures) do
      if tostring(k)==tostring(n.id) then info=n end 
    end 
    addSpriteFromBK(item, 1001, nameFromPath(info.headpic))
    local lb = CCLabelTTF:create(info.name, "Helvetica", 28) 
    local x,y = item:getChildByTag(1001):getPosition()
    lb:setPosition(ccp(x, y-30))
    item:addChild(lb)
    
    local tmp = v.c--UserData.userinfo.host.occupyInfo.occupy[tostring(v.id)].c
    TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setString(string.format("可征税: %d", tmp))

    item:removeChild(item:getChildByTag(1020), true) 
    local button = CCButton:createWithFileName("onCheFangButton", "btn_red_normal.png", "btn_red_highlight.png", "btn_disabled.png", "撤防")
    button:setPosition(ccp(220, 75))
    --button:setTag(tonumber(k))
    button:setTag(info.id)
    setObjectExtTable(button, self)
    item:addChild(button)
 

    --local info = UserData.userinfo.captures[c+1] 
    --local tmp = UserData.userinfo.host.occupyInfo.occupy[tostring(info.id)].e
    local dur = v.e - TimeManager.getServerTime()
    print("# dur", dur, c+1)
    
      button = CCButton:createWithFileName("onZhengShuiButton", "btn_green_normal.png", "btn_green_highlight.png", "btn_disabled.png", "征税")
      --button:setTag(10000+tonumber(k))
      button:setTag(10000+info.id)
      button:setPosition(ccp(220, 140))
      setObjectExtTable(button, self)
      item:addChild(button)
      self.buttons[c+1] = button
    if dur>0 then
      self:createUITimer(c+1, dur)
      button:setTitle("")
    end
    --end   
    c = c+1
  end
  end
  if self.table~=nil then
    self.node:removeChild(self.table, true)
  end
  local tableBK = self.node:getChildByTag(1010)
  local size = tableBK:getContentSize()
  local tableview = CCTableView:create(0, CCSizeMake(size.width-20, size.height), items)
  local x, y = ActionUtil.leftBottomPoint(tableBK)
  tableview:setPosition(x+10, y+20)
  self.node:addChild(tableview)
  self.table = tableview
end

ZhanFuYingSceneFunc.onZhenFuButton = function(self, node, event)
  print("ZhanFuYingSceneFunc.onZhenFuButton")
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
  NetworkRequest.randomConquer("onZhenFuComplete", self)
end
ZhanFuYingSceneFunc.onCheFangButton = function(self, node, event)
  NetworkRequest.retreat(node:getTag(), "onCheFangComplete", self)
  self.retreatId = node:getTag()
  LoadScene:showLoadingScene()
end
ZhanFuYingSceneFunc.onZhengShuiButton = function(self, node, event)
  NetworkRequest.gainTribute(node:getTag()-10000, "onZhengShuiComplete", self)
  LoadScene:showLoadingScene()
end
ZhanFuYingSceneFunc.onCheFangComplete = function(self, data)
  print("ZhanFuYingSceneFunc.onCheFangComplete", data)
  local res = cjson.decode(data)
  LoadScene:removeLoadingScene()
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
  elseif res.data.result==true then
    NetworkRequest.syncData(res)
    --UserData.userinfo.host.occupyInfo.occupy[tostring(self.retreatId)] = nil 
    --self:initTable()
    NetworkRequest.getUserInfo("onGetUserInfoComplete", self)
    LoadScene:showLoadingScene()
  end 
end
ZhanFuYingSceneFunc.onZhengShuiComplete = function(self, data)
  print("ZhanFuYingSceneFunc.onZhengShuiComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret~=0 then
    --showMessage("还不能征税", ccc3(255,255,0))
    showMessage(res.msg, ccc3(255,255,0))
  else
    NetworkRequest.syncData(res)
    showMessage("成功征税", ccc3(0,255,0))
  end 
end
ZhanFuYingSceneFunc.onZhenFuComplete = function(self, data)
  print("ZhanFuYingSceneFunc.onZhenFuComplete", data)
  LoadScene:removeLoadingScene() 
  local res = cjson.decode(data)
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
    setTouchEnabledAll(self.node, true)
  else
    self.conquerData = res
    local l = CCBReader:load("ZhanFuYing1Scene.ccbi")
    --setObjectExtTable(l:getChildByTag(1020), self)
    for k,v in ipairs(res.randomConquer) do
      local item = l:getChildByTag(2000+k)
      item:setVisible(true)
      local x,y = item:getChildByTag(1):getPosition()
      local imgName = nameFromPath(v.headpic)
      local button = CCButton:createWithFileName("onFightButton", imgName, imgName, imgName, "")
      setObjectExtTable(button, self)
      button:setPosition(ccp(x,y)) 
      button:setTag(k)
      item:addChild(button)

      TypeCast(item:getChildByTag(2), "CCLabelTTF"):setString(v.name)
      TypeCast(item:getChildByTag(3), "CCLabelTTF"):setString(string.format("Lv%d", v.level))
    end
    setObjectExtTable(l:getChildByTag(1111), self)
    --self.node:addChild(l)
    --self:clearUITimers()
    --MainScene.scene:removeChild(self.node, true) 
    self:closeScene()
    MainScene.scene:addChild(l)
    self.listLayer = l
  end
end

ZhanFuYingSceneFunc.onFightButton = function(self, node, event)
  if UserData.userinfo.host.soldiers>0 then
    LoadScene:showLoadingScene()
    --setTouchEnabledAll(self.listLayer, false) 
    NetworkRequest.occupy(self.conquerData.randomConquer[node:getTag()].id, "onFightComplete", self)
    self.occupyingName = self.conquerData.randomConquer[node:getTag()].name
    self.occupyingId = self.conquerData.randomConquer[node:getTag()].id
    MainScene.scene:removeChild(self.listLayer, true)
  else
    showMessage("兵力不足，请补充兵力！", ccc3(255,255,0))
  end
end
ZhanFuYingSceneFunc.onFightComplete = function(self, data)
  print("ZhanFuYingSceneFunc.onFightComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
    setTouchEnabledAll(self.listLayer, true)
  else
    NetworkRequest.syncData(res)
    MainScene.scene:removeChild(self.node, true)
    local war = WarScene:getNode(self.occupyingName, self, res)
    MainScene.scene:addChild(war.node)
  end
end
ZhanFuYingSceneFunc.fightEnd = function(self)
  --self:initTable()
  local l = ZhanFuYingScene:getNode()
  MainScene.scene:addChild(l.node)
  NetworkRequest.getUserInfo("onGetUserInfoComplete", l)
  LoadScene:showLoadingScene() 
end
ZhanFuYingSceneFunc.onUpgradeButton = function(self, node, event)
  if ZhanFuYingScene.upgradeCooldown==nil or ZhanFuYingScene.upgradeCooldown<=0 then
    --self.upBuildingButton:setEnabled(false)
    --setTouchEnabledAll(self.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.upgradeBuilding(BuildingID.ZhanFuYing, "onUpgradeZhanFuYingComplete", self)
  else
    --JiaSuDialog:create(BuildingID.ZhanFuYing,self)
  end
end
ZhanFuYingSceneFunc.onUpgradeZhanFuYingComplete = function(self, data) 
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage("升级成功", ccc3(0,255,0))
    
    local bl = UserData.userinfo.host.cities2[tostring(BuildingID.ZhanFuYing)].l
    local binfo = UserData.config.models[BuildingLevel[BuildingID.ZhanFuYing]][bl]
    UserData.userinfo.host.cities2[tostring(BuildingID.ZhanFuYing)].l = bl+1
    setBuildingBaseData(BuildingID.ZhanFuYing, self.node)
  
    self:initTable()
    
    self.upBuildingTimer = UITimer.createUITimer(string.format("upbuildingcd_%d", BuildingID.ZhanFuYing), binfo.coolingTime, self, ccp(160,72), ccp(0, 0))
    self.upBuildingTimer.view:setPosition(ccp(710, 420))
    self.node:addChild(self.upBuildingTimer.view)
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
  else
    showMessage(res.msg, ccc3(255,255,0))
    self.upBuildingButton:setEnabled(true)
    --local b = self.node:getChildByTag(ZhiHuiBuScene.NodeID.UpButton)
    --TypeCast(b, "CCButton"):setEnabled(true)
  end
  LoadScene:removeLoadingScene()
  self:closeScene()
  MainScene.scene:addChild(ZhanFuYingScene:getNode().node)
end
ZhanFuYingSceneFunc.clearUITimers = function(self, index)
  if self.uitimers~=nil then
    for k,v in pairs(self.uitimers) do
      UITimer.removeUITimer(v)
    end
  end
end
ZhanFuYingSceneFunc.createUITimer = function(self, index, dur)  
  local item = self.fuluItems[index]
  --TypeCast(item:getChildByTag(1020), "CCButton"):setEnabled(true)
  --TypeCast(item:getChildByTag(1020), "CCButton"):setVisible(true)
  
  local timer = UITimer.createUITimer(string.format("fulu_shoushui_%d", index), dur, self, ccp(-5,5), ccp(0, 0), true)
  --local timer = UITimer.createUITimer(string.format("fulu_shoushui_%d", index), 5, self, ccp(-5,5), ccp(0, 0), true)
  timer.view:setPosition(ccp(280, 120))
  item:addChild(timer.view)
  timer.index = index
  if self.uitimers==nil then
    self.uitimers = {}
  end
  self.uitimers[index] = timer
end
ZhanFuYingSceneFunc.onGetUserInfoComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    UserData.userinfo = res
  end
  self:initTable() 
end
---------------------------------------------------------------------------------------------------------------
--处理加速
ZhanFuYingSceneFunc.onTimerEnd = function(self, timer)
  if timer~=nil then
    timer.view:getParent():removeChild(timer.view, true)
  end
  if timer==self.upBuildingTimer then
    self.timeLabel:setVisible(true)
    self.upBuildingButton:setVisible(true)
    self.upBuildingButton:setEnabled(true)
  end
  if timer.index>0 then
    local button = self.buttons[timer.index]
    print("ZhanFuYingSceneFunc.onTimerEnd", timer.index, item)
    --TypeCast(item:getChildByTag(1020), "CCButton"):setEnabled(true)
    --TypeCast(item:getChildByTag(1020), "CCButton"):setVisible(true)
    TypeCast(button, "CCButton"):setTitle("征税")
  end
end

ZhanFuYingSceneFunc.onJiaSuButton = function(self, timer, pars)
  local tmp = JiaSuSampleDialog:create(pars.leftTime, self)
  MainScene.scene:addChild(tmp.view)
  UITimer.removeUITimer(self.upBuildingTimer)
  MainScene.scene:removeChild(self.node, true)
  self.jiasuUpBuildingDlg = tmp
end
ZhanFuYingSceneFunc.onJiaSuConfirm = function(self, dlg, confirm, timereel, timeLeft)
  if confirm~=true then
    MainScene.scene:removeChild(dlg.view, true)
    MainScene.scene:addChild(ZhanFuYingScene.getNode().node)
  elseif dlg==self.jiasuUpBuildingDlg then
    --NetworkRequest.test("onJiaSuComplete", self)
    NetworkRequest.accelerate("Upgrading", BuildingID.ZhanFuYing, timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene()
  end
end
ZhanFuYingSceneFunc.onJiaSuComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    if self.jiasuUpBuildingDlg~=nil then
      self.upBuildingTimer:endTimer()
    end
    showMessage("加速成功", ccc3(0,255,0))
  else
    showMessage(res.msg, ccc3(255,255,0))
    --showMessage("加速失败", ccc3(255,255,0))
  end
  if self.jiasuUpBuildingDlg~=nil then
    MainScene.scene:removeChild(self.jiasuUpBuildingDlg.view, true)
  end
  MainScene.scene:addChild(ZhanFuYingScene.getNode().node)
end
ZhanFuYingSceneFunc.onCloseZhanFuYing1 = function(self, node, event)
  --setTouchEnabledAll(self.node, true)
  node:getParent():getParent():removeChild(node:getParent(), true)
  MainScene.scene:addChild(ZhanFuYingScene:getNode().node)
end
ZhanFuYingSceneFunc.closeScene = function(self)
  UITimer.removeUITimer(self.upBuildingTimer)
  self:clearUITimers()
  MainScene.scene:removeChild(self.node, true)
  ZhanFuYingScene.instance = nil
end
ZhanFuYingSceneFunc.onClose = function(self, node, event)
  print("onCloseZhiHuiBu")
  self:closeScene()
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
end

